package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.DownloadAudioModel;
import com.banshuibao.bsb_android.model.DownloadInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.bokecc.sdk.mobile.download.Downloader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class DownloadingAudioAdapter extends BaseAdapter {

    private List<DownloadAudioModel> pairs;

    private Context context;

    private boolean isShowCheck;

    public DownloadingAudioAdapter(Context context) {
        this.pairs = new ArrayList<>();
        this.context = context;
    }

    public void setData(List<DownloadAudioModel> list) {
        if (this.pairs != null) {
            this.pairs.clear();
            this.pairs.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return pairs.size();
    }

    @Override
    public DownloadAudioModel getItem(int position) {
        return pairs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_download_audio, parent, false);
            viewHolder.img = convertView.findViewById(R.id.img);
            viewHolder.name = convertView.findViewById(R.id.name);
            viewHolder.size = convertView.findViewById(R.id.size);
            viewHolder.check = convertView.findViewById(R.id.check_iv);
            viewHolder.progressBar = convertView.findViewById(R.id.progress_bar);
            viewHolder.download_status = convertView.findViewById(R.id.download_status);
            viewHolder.duty = convertView.findViewById(R.id.duty);
            viewHolder.question = convertView.findViewById(R.id.question);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DownloadAudioModel data = getItem(position);
        if (data != null) {
            viewHolder.name.setText(data.getName());
            viewHolder.duty.setText(data.getDuty());
            if (!TextUtils.isEmpty(data.getImg())) {
                byte[] bitmapArray = Base64.decode(data.getImg(), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
                viewHolder.img.setImageBitmap(bitmap);
            } else
                viewHolder.img.setImageResource(R.drawable.login_iv);
            if (isShowCheck)
                viewHolder.check.setVisibility(View.VISIBLE);
            else
                viewHolder.check.setVisibility(View.GONE);
            if (data.isCheck()) {
                viewHolder.check.setImageResource(R.drawable.icon_video_selected);
            } else {
                viewHolder.check.setImageResource(R.drawable.icon_video_no_selected);
            }
            if (data.getStatus() == DownloadAudioModel.DOWNLOAD) {
                viewHolder.download_status.setImageResource(R.drawable.icon_download_pause);
            } else {
                viewHolder.download_status.setImageResource(R.drawable.icon_download_start);
            }
            viewHolder.progressBar.setProgress((int) data.getProgress());
            viewHolder.question.setText(data.getQuestion());
            viewHolder.size.setText(data.getProgressText());
        }
        return convertView;

    }

    public void setShowCheck(boolean showCheck) {
        isShowCheck = showCheck;
        notifyDataSetChanged();
    }

    public List<DownloadAudioModel> getList() {
        return pairs;
    }

    public void clear() {
        if (pairs != null) {
            pairs.clear();
            pairs = null;
        }
    }

    private class ViewHolder {
        ImageView img, check, download_status;
        TextView name, size, duty, question;
        ProgressBar progressBar;
    }

}
